import {insertPeron, deletePerson, selectPersons, updatePerson} from "@/services/person/person";
// select, delete, insert, update
// fetch , remove , add , modify

export default {
  namespace: "person",

  state: {
    personList: [],
  },

  effects: {
    *fetchPersons(_, {call, put}) {
      const res = yield call(selectPersons);
      // 调用reducer
      yield put({
        type: "setPersonList",
        payload: res.data
      })
    },

    * modifyPerson({payload, callback}, {call, put}) {
      const res = yield call(updatePerson, payload);
      callback(res.msg);
      yield put({
        type: "modifyPersonList",
      })
    },

    * addPerson({payload, callback}, {call, put}) {
      const res = yield call(insertPeron, payload);
      callback(res.msg);
      yield put({
        type: "addPersonList",
      })
    },

    * removePerson({payload, callback}, {call, put}) {
      const res = yield call(deletePerson, payload);
      callback(res.msg);
      yield put({
        type: "removePersonList"
      });
    }
  },

  // 更新state
  reducers: {
    setPersonList(state, action) {
      return {
        ...state,
        personList: action.payload
      }
    },
    addPersonList(state, action) {
      return {...state};
    },
    removePersonList(state, action) {
      return {...state};
    },
    modifyPerson(state, action) {
      return {...state};
    }
  }
}
